<template>
    <div>
        <Header></Header>
        <div class="content">
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                <el-form-item label="标题" prop="title">
                    <el-input v-model="ruleForm.title"></el-input>
                </el-form-item>

                <el-form-item label="摘要" prop="description">
                    <el-input type="textarea" v-model="ruleForm.description"></el-input>
                </el-form-item>
                <el-form-item label="文章" prop="content" class>
                    <mavon-editor v-model="ruleForm.content"></mavon-editor>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submitForm('ruleForm')">立即发表</el-button>
                    <el-button @click="resetForm('ruleForm')">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
    import Header from '../components/Header'
    export default {
        name: "BlogEdit",
        components: {Header},
        data() {
            return {
                ruleForm: {
                    id:'',
                    title: '',
                    description: '',
                    content:''
                },
                rules: {
                    title: [
                        { required: true, message: '请输入标题', trigger: 'blur' },
                    ],
                    description: [
                        { required: true, message: '请填写摘要', trigger: 'blur' }
                    ],
                    content: [
                        { required: true, message: '多多少少写点东西吧', trigger: 'blur' }
                    ]
                }
            };
        },
        methods: {
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        const _this = this
                        _this.$axios.post('/blog/edit',_this.ruleForm,{
                            headers:{
                                'Authorization':localStorage.getItem('token')
                            }
                        }).then(res=>{
                            console.log(res)
                            _this.$router.push('/blogs')
                        })
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            }
        },
        //如果请求的地址是编辑的话，那就做一个页面的回显
        created() {
            const blogId = this.$route.params.blogId
            const _this = this
            if (blogId){
                _this.$axios.get('/blog/detail/'+blogId).then(res=>{
                    _this.ruleForm.id = res.data.data.id
                    _this.ruleForm.content = res.data.data.content
                    _this.ruleForm.title = res.data.data.title
                    _this.ruleForm.description = res.data.data.description
                })
            }
        }
    }
</script>

<style scoped>
.content{
    text-align: center;
}
</style>